<!--
 * @Description: 
 * @Author: zhihong deng
 * @Date: 2021-05-30 14:47:01
 * @LastEditors: zhihong deng
 * @LastEditTime: 2021-07-12 16:46:09
-->
<template>
    <div class="com-container">
        <div class="title" :style="comStyle">
            <span>{{ '▍' + showTitle }}</span>
            <span class="iconfont title-icon" :style="comStyle" @click="showChoice = !showChoice">&#xe6eb;</span>
            <div class="select-con" v-show="showChoice" :style="marginStyle">
                <div class="select-item" v-for="item in selectTypes" :key="item.key" @click="handSelect(item.key)">
                    {{ item.text }}
                </div>
            </div>
        </div>
        <div class="com-chart" ref="trend_ref"></div>
    </div>
</template>

<script>
import { mapState } from 'vuex'
import { getThemeValue } from '../utils/theme_utils.js'
export default {
    data () {
        return {
            chartInstance: '',//Echarts图实例
            allData: '',
            showChoice: false,//是否显示可选项
            choiceType: 'map',
            titleFonstSize: 0,
        }
    },
    methods: {
        initChart () {
            this.chartInstance = this.$echarts.init(this.$refs.trend_ref,this.theme);
            const initOption = {
                grid: {
                    left: '3%',
                    top: '35%',
                    right: '4',
                    bottom: '1%',
                    containLabel: true
                },
                // 鼠标移入提示
                tooltip: {
                    trigger: 'axis',//移入轴提示
                },
                legend: {
                    left: 20,
                    top: '15%',
                    icon: 'circle'
                },
                xAxis: {
                    type: 'category',
                    boundaryGap: false,//紧挨边缘
                },
                yAxis: {
                    type: 'value',
                },
            };
            this.chartInstance.setOption(initOption);
        },
        // ret 就是服务端发送给客户端的图表的数据
        getData (ret) {
            // const { data: ret } = await this.$http.get('trend');
            this.allData = ret;
            console.log('ret',this.allData);
            this.updateChart();
        },
        // 更新图表
        updateChart () {
            //半透明的颜色值
            const colorArr1 = [
                'rgba(11, 168, 44, 0.5)',
                'rgba(44,110, 255, 0.5)',
                'rgba(22, 242, 217, 0.5)',
                'rgba( 254, 33, 30, 0.5)',
                'rgba(250, 105, 0, 0.5)'
            ]
            //全透明的颜色值
            const colorArr2 = [
                'rgba(11, 168, 44, 0)',
                'rgba(44, 110, 255, 0)',
                'rgba(22, 242, 217, 0)',
                'rgba(254, 33, 30, 0)',
                'rgba(250, 105, 0, 0)'
            ]
            // 类目轴的数据
            const timeArr = this.allData.common.month;
            // y轴的数据 serise 下的数据
            const valueArr = this.allData[ this.choiceType ].data;
            const seriesArr = valueArr.map((item,index) => {
                return {
                    name: item.name,
                    type: 'line',
                    data: item.data,
                    stack: this.choiceType,
                    areaStyle: {
                        color: new this.$echarts.graphic.LinearGradient(0,0,0,1,[
                            //0%的颜色
                            {
                                offset: 0,
                                color: colorArr1[index]
                            },
                            //100%的颜色
                            {
                                offset: 1,
                                color: colorArr2[index]
                            },
                        ]),
                    },
                }
            });
            // 图例的数据
            const legendArr = valueArr.map(item => {
                return item.name
            });
            const dataOption = {
                legend: {
                    data: legendArr
                },
                xAxis: {
                    data: timeArr
                },
                series: seriesArr,
            };
            this.chartInstance.setOption(dataOption);
        },
        // 屏幕适配
        screenAdapter () {
            this.titleFonstSize = this.$refs.trend_ref.offsetWidth / 100 * 3.6;
            const adapterOption = {
                legend: {
                    itemWidth: this.titleFonstSize,
                    itemHeight: this.titleFonstSize,
                    itemGap: this.titleFonstSize,
                    textStyle: {
                        fontSize: this.titleFonstSize / 2,
                    }
                },
                // xAxis: {
                //     axisLabel: {
                //         fontSize: this.titleFonstSize,
                //     }
                // },
                // yAxis: {
                //     axisLabel: {
                //         fontSize: this.titleFonstSize,
                //     }
                // },
            };
            this.chartInstance.setOption(adapterOption);
            this.chartInstance.resize();
        },
        // 点击选择按钮
        handSelect (currentType) {
            this.choiceType = currentType;
            this.showChoice = false;
            this.updateChart();
        },
    },
    computed: {
      ...mapState(['theme']),
      selectTypes () {
          if(!this.allData){
              return [];
          }else{
              return this.allData.type.filter(item => {
                  return item.key !== this.choiceType;//可选值不等于当前值
              });
          }
      },
      showTitle () {
          if (!this.allData) {
              return ''
          } else {
              return this.allData[this.choiceType].title;
          }
      },
      comStyle () {
          return {
              fontSize: this.titleFonstSize + 'px',
              color: getThemeValue(this.theme).titleColor,
          }
      },
      marginStyle () {
          return {
              marginLeft: this.titleFonstSize + 'px',
              backgroundColor: getThemeValue(this.theme).backgroundColor,
          }
      }
    },
    watch: {
      theme () {
        // 第一步：销毁当前图表
        this.chartInstance.dispose()
        // 第二步：重新以最新的主题初始化图表对象
        this.initChart()
        // 第三步：完成屏幕的适配
        this.screenAdapter()
        // 第四步：更新图表的展示
        this.updateChart()
      }
    },
    created () {
      // 在组件创建完成之后进行回调函数的注册
      this.$socket.registerCallBack('trendData',this.getData)
    },
    mounted () {
        this.initChart();
        // this.getData();
        // 发送数据给服务器，告诉服务器我现在需要数据
        this.$socket.send({
          action: 'getData',
          socketType: 'trendData',
          chartName: 'trend',
          value: ''
        })
        window.addEventListener('resize',this.screenAdapter);
        this.screenAdapter();
    },
    destroyed () {
        // 取消屏幕适配监听
        window.removeEventListener('resize',this.screenAdapter);
        // 在组件销毁的时候，进行回调函数的取消
        this.$socket.unRegisterCallBack('trendData')
    },
}
</script>

<style lang="less" scoped>
.title{
    position: absolute;
    z-index: 10;
    top: 20px;
    left: 20px;
    color: white;
    .title-icon{
        margin-left: 10px;
        cursor: pointer;    
    }
    .select-con{
        cursor: pointer;
        background-color: #222733;
        .select-item{
            cursor: pointer;
        }
    }
}
</style>